package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.common.api.ResultCode;
import com.macro.mall.tiny.modules.pms.model.PmsControlSkill;
import com.macro.mall.tiny.modules.pms.model.PmsControlSkillBrand;
import com.macro.mall.tiny.modules.pms.service.PmsControlSkillBrandService;
import com.macro.mall.tiny.modules.pms.service.PmsControlSkillService;
import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.model.UmsMember;
import com.macro.mall.tiny.security.util.AdminContext;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 工控技术表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2025-03-02
 */
@RestController
@RequestMapping("/pms/pmsControlSkill")
@Tag(name = "PmsControlSkillController", description = "工控技术管理")
@Api(tags = "PmsControlSkillController")
public class PmsControlSkillController {

    @Autowired
    private PmsControlSkillBrandService pmsControlSkillBrandService;

    @Autowired
    private PmsControlSkillService pmsControlSkillService;

    @GetMapping("/brand")
    @ApiOperation("获取所有品牌")
    public CommonResult<List<PmsControlSkillBrand>> listBrand(@RequestParam(required = false) String keyword) {
        if (StringUtils.isNotEmpty(keyword)) {
            QueryWrapper<PmsControlSkillBrand> wrapper = new QueryWrapper<>();
            wrapper.like("name", keyword);
            return CommonResult.success(pmsControlSkillBrandService.list(wrapper));
        }
        return CommonResult.success(pmsControlSkillBrandService.list());
    }


    @GetMapping("/list")
    @ApiOperation("获取所有技术")
    public CommonResult<List<PmsControlSkill>> listControlSkill(@RequestParam Integer brandId,
                                                                @RequestParam(required = false) String item) {
        QueryWrapper<PmsControlSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        if (StringUtils.isNotEmpty(item)) {
            wrapper.eq("type", item);
        }
        wrapper.groupBy("item");
        List<PmsControlSkill> list = pmsControlSkillService.list(wrapper);
        return CommonResult.success(list);
    }

    @GetMapping("/listByItem")
    @ApiOperation("获取所有技术")
    public CommonResult<List<PmsControlSkill>> listControlSkillByItem(@RequestParam Integer brandId,
                                                                      @RequestParam String item,
                                                                      @RequestParam(required = false) String keyword) {
        QueryWrapper<PmsControlSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        wrapper.eq("item", item);
        if (StringUtils.isNotEmpty(keyword)) {
            wrapper.like("code", keyword);
        }
        wrapper.groupBy("code");
        List<PmsControlSkill> list = pmsControlSkillService.list(wrapper);
        return CommonResult.success(list);
    }

    @ApiOperation("获取技术详情")
    @GetMapping("/getDetail")
    public CommonResult<List<PmsControlSkill>> getControlSkillDetail(@RequestParam Integer brandId,
                                                                     @RequestParam String item,
                                                                     @RequestParam(required = false) String code,
                                                                     @RequestParam(required = false) String keyword) {
        UmsMember member = MemberContext.getMember();
        if (member == null) {
            return CommonResult.unauthorized(null);
        }
        if (member.getVip() != 1) {
            return CommonResult.failed(ResultCode.NEED_VIP);
        }
        QueryWrapper<PmsControlSkill> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        wrapper.eq("item", item);
        if (StringUtils.isNotEmpty(keyword)) {
            wrapper.like("code", keyword);
        }
        if (StringUtils.isNotEmpty(code)) {
            wrapper.like("code", code);
        }

        return CommonResult.success(pmsControlSkillService.list(wrapper));
    }


    @PostMapping("/page")
    @ApiOperation("分页获取技术")
    public CommonResult<Page<PmsControlSkill>> listControlSkill(@RequestBody PmsControlSkill pmsControlSkill) {
//        UmsAdmin umsAdmin = AdminContext.getHolder().get();
//        if (umsAdmin == null) {
//            return CommonResult.unauthorized(null);
//        }
        Map<Long, String> collect = pmsControlSkillBrandService.list().stream().collect(Collectors.toMap(PmsControlSkillBrand::getId, PmsControlSkillBrand::getName));
        Page<PmsControlSkill> page = new Page<>(pmsControlSkill.getPageNum(), pmsControlSkill.getPageSize(), Boolean.TRUE);
        QueryWrapper<PmsControlSkill> query = new QueryWrapper<>();
        if (pmsControlSkill.getBrandId() != null) query.eq("brand_id", pmsControlSkill.getBrandId());
        if (pmsControlSkill.getType() != null) query.eq("type", pmsControlSkill.getType());
        if (pmsControlSkill.getStatus() != null) query.eq("status", pmsControlSkill.getStatus());
        if (pmsControlSkill.getId() != null) query.eq("id", pmsControlSkill.getId());
        if (pmsControlSkill.getCode() != null) query.like("code", pmsControlSkill.getCode());
        if (pmsControlSkill.getItem() != null) query.like("item", pmsControlSkill.getItem());
        query.orderByDesc("id");
        Page<PmsControlSkill> result = pmsControlSkillService.page(page, query);
        for (PmsControlSkill record : result.getRecords()) {
            record.setBrandName(collect.get(record.getBrandId()));
        }
        return CommonResult.success(result);
    }

    @PostMapping("/add")
    @ApiOperation("添加技术")
    public CommonResult<Void> addControlSkill(@RequestBody PmsControlSkill pmsControlSkill) {
//        UmsAdmin umsAdmin = AdminContext.getHolder().get();
//        if (umsAdmin == null) {
//            return CommonResult.unauthorized(null);
//        }
        pmsControlSkill.setStatus(1);
        pmsControlSkillService.save(pmsControlSkill);
        return CommonResult.success(null);
    }

    @PostMapping("/update")
    @ApiOperation("修改技术")
    public CommonResult<Void> updateControlSkill(@RequestBody PmsControlSkill pmsControlSkill) {
//        UmsAdmin umsAdmin = AdminContext.getHolder().get();
//        if (umsAdmin == null) {
//            return CommonResult.unauthorized(null);
//        }
        pmsControlSkillService.updateById(pmsControlSkill);
        return CommonResult.success(null);
    }

    @PostMapping("/delete")
    @ApiOperation("删除技术")
    public CommonResult<Void> deleteControlSkill(@RequestBody List<Integer> ids) {
//        UmsAdmin umsAdmin = AdminContext.getHolder().get();
//        if (umsAdmin == null) {
//            return CommonResult.unauthorized(null);
//        }
        pmsControlSkillService.removeByIds(ids);
        return CommonResult.success(null);
    }


}

